<?php
namespace app\shop\controller;
use app\common\controller\Common;
use app\shop\model\PurchaseModel;
use app\common\model\Category;
use app\common\model\Supplier;
use app\common\model\News;
use think\Exception;
use think\Request;

/**
* 采购产品
*/
class Purchase extends Common
{
	/**
	* 添加
	*/
	public function addpurchase()
	{
		if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(PurchaseModel::setPurchase($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
		$this->assign('supp',(new Supplier())->supplierList(['is_show'=>0],'supplier_id,supplier_name'));
		$this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
		$this->assign('news',(new News())->newsList(['type'=>1]));
		return $this->fetch();
	}
	/**
	* 列表
	*/
	public function purchaselist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			return PurchaseModel::getPurchaseList($info);
		}
		return $this->fetch();
	}
    /**
    * 总部采购单列表
    */
    public function purquarterslist()
	{
		if(request()->isPost())
		{
			$info = Request::instance()->param();
			return PurchaseModel::getPurchaseList($info,true);
		}
        $this->assign('fcat',(new Category())->categoryList(['fid'=>0],'category_id,category_name'));
		return $this->fetch();
	}
    /**
    * 采购单详情
    */
    public function purquartersinfo()
    {
        $info = Request::instance()->param();
        $Purchase = PurchaseModel::getPurchaseInfo($info);
        $this->assign('purchase',$Purchase);
        return $this->fetch();
    }
    /**
    * 修改采购单状态
    */
    public function purstate()
    {
        if(request()->isPost())
		{
			try{
				$info = Request::instance()->param();
				return json(PurchaseModel::purState($info));
			}catch (\Exception $e){
				return json(back(config('state.STATUS_CODE_FOURONE'),$e->getMessage()));
			}
		}
    }
}
